'use strict';
const Controller = require('egg').Controller;
const Error = require('../../class/error')
class ArticleTypeController extends Controller {
  async list() {
    const ctx = this.ctx;
    const data = await this.service.articleType.list()
    ctx.locals = {
      articleTypes: data,
    }
    await ctx.render('admin/articleType/list.html');
  }

  /**
   * @description 删除
   * @author xiezuobing[948466]<435321508@qq.com>
   * @memberof ArticleTypeController
   */
  async deleteJson() {
    const ctx = this.ctx;
    try {
      await this.service.articleType.delete(ctx.query.articleTypeId);
      ctx.body = Error.SUCCESS;
    } catch (e) {
      ctx.body = Error.UNKOWN;
    }
  }

  /**
   * @description 增加
   * @author xiezuobing[948466]<435321508@qq.com>
   * @returns  
   * @memberof ArticleTypeController
   */
  async addOrEditJson() {
    const ctx = this.ctx;
    const query = ctx.query;
    let values = {
      name: query.name,
      img: query.img,
      parent_id: +query.parent_id,
      grand_parent_id: +query.grand_parent_id,
      create_time: new Date()
    };
    if (query.id) {
      values.id = +query.id;
      await this.service.articleType.update(values);
      ctx.body = await this.service.articleType.selectById(values.id);
    } else {
      ctx.body = await this.service.articleType.create(values);
    }

  }

}

module.exports = ArticleTypeController;

